package com.example.dllo.keepdemo.train.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.example.dllo.keepdemo.R;
import com.example.dllo.keepdemo.base.BaseActivity;
import com.example.dllo.keepdemo.tools.GsonRequest;
import com.example.dllo.keepdemo.tools.VolleySingleton;
import com.example.dllo.keepdemo.train.adapter.TrainPlanExpandableListViewAdapter;
import com.example.dllo.keepdemo.train.bean.TrainPlanBean;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

/**
 * Created by dllo on 16/1/13.
 */
public class TrainPlanActivity extends BaseActivity implements View.OnClickListener, View.OnTouchListener {
    private ExpandableListView listView;
    private TrainPlanExpandableListViewAdapter adapter;
    private TextView tvAdd;
    private ScrollView scrollView;
    private ImageView imgDetail;
    private TrainPlanBean.DataEntity dataEntity;
    private TextView tvTitle, tvSubtitle, tvDifficulty, tvFoundations, tvBody, tvTime, tvEveryday, tvEnergy, tvJoin, tvBackground;
    private RatingBar ratingBar;
    private ImageView imgBackground;
    private Button btnBack, btnShare;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trainplan);
        listView = (ExpandableListView) findViewById(R.id.expandablelistview_activity_train_plan);
        tvAdd = (TextView) findViewById(R.id.tv_activity_train_plan_add);
        scrollView = (ScrollView) findViewById(R.id.scroll_activity_train_plan);
        imgDetail = (ImageView) findViewById(R.id.img_activity_train_plan_detail);
        tvTitle = (TextView) findViewById(R.id.tv_activity_train_plan_title);
        tvSubtitle = (TextView) findViewById(R.id.tv_activity_train_plan_subtitle);
        tvDifficulty = (TextView) findViewById(R.id.tv_activity_train_plan_difficulty);
        tvFoundations = (TextView) findViewById(R.id.tv_activity_train_plan_foundations);
        tvBody = (TextView) findViewById(R.id.tv_activity_train_plan_whole_body);
        tvTime = (TextView) findViewById(R.id.tv_activity_train_plan_whole_time);
        tvEveryday = (TextView) findViewById(R.id.tv_activity_train_plan_everyday);
        tvEnergy = (TextView) findViewById(R.id.tv_activity_train_plan_energy);
        tvJoin = (TextView) findViewById(R.id.tv_activity_train_plan_join_person_number);
        ratingBar = (RatingBar) findViewById(R.id.rating_activity_train_plan_star);
        imgBackground = (ImageView) findViewById(R.id.img_activity_train_plan_background);
        btnBack = (Button) findViewById(R.id.btn_activity_train_plan_back);
        btnShare = (Button) findViewById(R.id.btn_activity_train_plan_share);
        tvBackground = (TextView) findViewById(R.id.tv_activity_train_plan_background);

        tvAdd.setOnClickListener(this);
        imgDetail.setOnClickListener(this);
        btnBack.setOnClickListener(this);
        btnShare.setOnClickListener(this);
        //去掉箭头
        listView.setGroupIndicator(null);
        //去下划线
        listView.setDivider(null);
        scrollView.smoothScrollTo(0, 0);
        scrollView.setOnTouchListener(this);

        initData();

    }

    private void loadData() {
        tvTitle.setText(dataEntity.getName());
        tvSubtitle.setText(dataEntity.getDescription());
        tvDifficulty.setText("K" + dataEntity.getDifficulty());
        tvBody.setText(dataEntity.getTrainingpoints().get(0).getName());
        tvTime.setText(dataEntity.getDays() + "");
        tvEveryday.setText(dataEntity.getAverageDuration() + "");
        tvEnergy.setText(dataEntity.getCalorie() + "");
        tvJoin.setText(dataEntity.getPioneer() + "人已参加");
        ratingBar.setRating((float) dataEntity.getDifficulty());
        switch (dataEntity.getDifficulty()) {
            case 1:
                tvFoundations.setText("零基础");
                break;
            case 2:
                tvFoundations.setText("初学");
                break;
            case 3:
                tvFoundations.setText("进阶");
                break;
            case 4:
                tvFoundations.setText("强化");
                break;
            case 5:
                tvFoundations.setText("挑战");
                break;
        }
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(imgBackground, R.mipmap.planmain320_370, R.mipmap.no_photo);
        VolleySingleton.getImageLoader().get(dataEntity.getPicture(), listener, 400, 460);
    }

    private void initData() {
        dataEntity = new TrainPlanBean.DataEntity();
        Log.d("TrainPlanActivity", "initdata");
        Intent intent = getIntent();
        String _id = intent.getStringExtra("id");
        String url = "https://api.gotokeep.com/v1.1/plans/" + _id;
        Log.d("TrainPlanActivity", url);

        GsonRequest<TrainPlanBean> gsonRequest = new GsonRequest<TrainPlanBean>(Request.Method.GET, url, TrainPlanBean.class, new Response.Listener<TrainPlanBean>() {
            @Override
            public void onResponse(TrainPlanBean response) {
                dataEntity = response.getData();
                Log.d("TrainPlanActivity", "dataEntity:" + dataEntity);
                adapter = new TrainPlanExpandableListViewAdapter(TrainPlanActivity.this, dataEntity);
                listView.setAdapter(adapter);
                loadData();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("TrainPlanActivity", "errorerrorerror");
            }
        });
        VolleySingleton.getInstance()._addRequest(gsonRequest);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_activity_train_plan_add:
                toast("添加");
                break;
            case R.id.img_activity_train_plan_detail:
                Intent intent = new Intent(this, PlanDetailActivity.class);
                intent.putExtra("detail", dataEntity.getDetail());
                startActivity(intent);
                break;
            case R.id.btn_activity_train_plan_back:
                TrainPlanActivity.this.finish();
                break;
            case R.id.btn_activity_train_plan_share:

                ShareSDK.initSDK(this);
                OnekeyShare oks = new OnekeyShare();
                //关闭sso授权
                oks.disableSSOWhenAuthorize();

                // 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
                //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
                // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
                oks.setTitle(getString(R.string.share));
                // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
                oks.setTitleUrl("http://sharesdk.cn");
                // text是分享文本，所有平台都需要这个字段
                oks.setText("我是分享文本");
                // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
                oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
                // url仅在微信（包括好友和朋友圈）中使用
                oks.setUrl("http://sharesdk.cn");
                // comment是我对这条分享的评论，仅在人人网和QQ空间使用
                oks.setComment("我是测试评论文本");
                // site是分享此内容的网站名称，仅在QQ空间使用
                oks.setSite(getString(R.string.app_name));
                // siteUrl是分享此内容的网站地址，仅在QQ空间使用
                oks.setSiteUrl("http://sharesdk.cn");

                // 启动分享GUI
                oks.show(this);

            break;
        }
    }

    //scrollview的滑动监听
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                int scrollY = v.getScrollY();
                if (scrollY == 0) {
                    tvBackground.setAlpha(0);
                } else {
                    tvBackground.setAlpha(1);
                }
                break;
        }
        return false;
    }
}
